import store from '@/store/index.js';
import { getUserInfoById, logout } from '@/config/api/user.js'
export default {
	data() {
		return {
			userInfo: store.state.$userInfo,
			token: store.state.$token,
			userID: store.state.$userID
		}
	},
	methods: {
		updateUserInfo(newData) {
			store.dispatch('setUserInfoAction', newData)
		},

		// 请求获取用户信息
		requestUserInfo() {
			console.log(111);
			const config = { custom: { catch: true } }
			getUserInfoById(this.userID, config)
				.then(res => {
					res.avatar = uni.$u.http.config.baseURL + res.avatar
					this.userInfo=res
					this.userInfo.avatar = res.avatar
					this.updateUserInfo(res)
				})
				.catch(err => {
					// 报错 登出
					console.log(err)
					this.logoutFun()
				})
		},

		// 手机号码加密
		phoneNumberFormat(value = "") {
			if (!value || typeof value !== 'string') return ""
			return value ? value.substring(0, 3) + "****" + value.substring(7) : ""
		},

		async logoutFun() {
			await logout()

			uni.$u.toast("登出成功")
			store.dispatch("logout")

			uni.navigateTo({
				url: "/pages/login/login"
			})
		}
	}
}